//
//  RJCheckDataSelectView.m
//  AirQuality
//
//  Created by Po on 15/5/4.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJCheckDataSelectView.h"
@interface RJCheckDataSelectView ()

@property (retain, nonatomic) UIView * selectView;      //选取区域
@property (retain, nonatomic) UILabel * titleLabel;     //标题
@property (retain, nonatomic) NSArray * titles;
@property (retain, nonatomic) NSMutableArray * selectButtonArray;
- (void)initInterface;
@end

@implementation RJCheckDataSelectView

- (instancetype)initWithTitles:(NSArray *)titles
{
    self = [super initWithFrame:ScreenBounds];
    if (self) {
        _titles = [NSArray arrayWithArray:titles];
        [self initInterface];
    }
    return self;
}

- (void)initInterface
{
    [self setBackgroundColor:[UIColor colorWithWhite:0.3 alpha:0.3]];
    [self addSubview:[self getSelectView]];
    [self initAllButton];
    
}

- (UIView *)getSelectView
{
    if (!_selectView) {
        _selectView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth - 100, 44 * (_titles.count + 2))];
        [_selectView setCenter:self.center];
        [_selectView setBackgroundColor:[UIColor whiteColor]];
        [_selectView.layer setBorderColor:[UIColor whiteColor].CGColor];
        [_selectView.layer setBorderWidth:1];
        [_selectView addSubview:[self getTittleLabel]];
    }
    return _selectView;
}

- (UILabel *)getTittleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _selectView.frame.size.width, 44)];
        [_titleLabel setBackgroundColor:GetRGBColor(79, 180, 255, 1)];
        [_titleLabel setTextAlignment:NSTextAlignmentCenter];
        [_titleLabel setText:@"筛选"];
    }
    return _titleLabel;
}

- (void)initAllButton
{
    CGFloat width = _selectView.frame.size.width / 2;
    CGFloat height = _selectView.frame.size.height / (_titles.count + 2);
    
    _selectButtonArray = [NSMutableArray array];
    
    for (NSUInteger i = 0; i < _titles.count + 2; i ++) {
        UIButton * button;
        if (i < _titles.count) {
            button = [[UIButton alloc] initWithFrame:CGRectMake(width / 2, 44 + height * i, width , height)];
            [button setImage:[UIImage imageNamed:@"selectNormal"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"selectSelect"] forState:UIControlStateSelected];
            [button setImageEdgeInsets:UIEdgeInsetsMake(0, 20, 0, 0)];
            
            [button setTitle:[NSString stringWithFormat:@"%@",_titles[i]] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [button setTitleEdgeInsets:UIEdgeInsetsMake(0, width / 4, 0, 0)];
            [button.titleLabel setTextAlignment:NSTextAlignmentCenter];
            [button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
            [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
            [_selectView addSubview:button];
        } else {
            button = [[UIButton alloc] initWithFrame:CGRectMake(width * (i - _titles.count), height * (_titles.count + 1), width , height)];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
            if (i != _titles.count) {
                [button setBackgroundColor:GetRGBColor(79, 180, 255, 1)];
                [button setTitle:@"确定" forState:UIControlStateNormal];
                [button addTarget:self action:@selector(pressSelectConfirmButton:) forControlEvents:UIControlEventTouchUpInside];
            } else {
                [button setBackgroundColor:[UIColor whiteColor]];
                [button setTitle:@"取消" forState:UIControlStateNormal];
                [button addTarget:self action:@selector(pressSelectCancelButton:) forControlEvents:UIControlEventTouchUpInside];
            }
        }
        [_selectButtonArray addObject:button];
        [_selectView addSubview:button];
    }
}

- (void)setSelectTypes:(NSArray *)types
{
    if (types == nil) {
        return;
    }
    NSArray * typesTemp = [RJTools getTitlesArrayWithKeys:types];
    for (NSString * title in typesTemp) {
        NSInteger count = [_titles indexOfObject:title];
        if (count != NSNotFound) {
            UIButton * button = _selectButtonArray[count];
            [button setSelected:YES];
        }
    }
}

#pragma mark - event
- (void)pressButton:(UIButton *)sender
{
    BOOL onOff = !sender.selected;
    sender.selected = onOff;
}

- (void)pressSelectConfirmButton:(UIButton *)sender
{
    NSArray * keys = [self getSelectKeyArray];
    if (_delegate) {
        [_delegate CheckDataSelectView:self didEndSelectWithTitle:keys];
    }
    [self removeSelf];
}

- (void)pressSelectCancelButton:(UIButton *)sender
{
    [self removeSelf];
}

#pragma mark - function
- (NSArray *)getSelectKeyArray
{
    NSMutableArray * selectTitles = [NSMutableArray array];
    for (UIButton * button in _selectButtonArray) {
        if (button.selected) {
            [selectTitles addObject:button.titleLabel.text];
        }
    }
    return [RJTools getKeysArrayWithTitles:selectTitles];
}
/**结束选取*/
- (void)removeSelf
{
    [self removeFromSuperview];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self];
    CGRect rect = _selectView.frame;
    if (point.x > rect.origin.x && point.x < (rect.origin.x + rect.size.width)
        && point.y < (rect.origin.y + rect.size.height) && point.y > rect.origin.y) {
        return;
    }
    [self removeSelf];
}

@end
